<?php
/**
 * user: daishiqi
 * Date：2021/6/25
 * Time: 10:48 上午
 */

namespace App\Console\Commands;


use App\Http\Model\FinanceWithdrawOrder;
use App\Lib\CloudAccount;
use App\Lib\CloudAccountRsa;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class UpdateCloudAccountStatus extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'query:cloud-account-status';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '主动查询云账户订单状态';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }


    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //云账户，进行中的订单，时间大于20200101

        $list = DB::table('finance_withdraw_order')
            ->where(['status' => 3, 'pay_type' => 3])
            ->where('create_timestamp', '>',1577808000)
            ->get();
        if (!$list) {
            return $this->line('无相关状态订单');
        }

        $cloud = CloudAccountRsa::getInstance(2);

        foreach ($list as $k => $v) {
            $res = $cloud->queryOrderStatus($v->order_sn);
            Log::info(__METHOD__ .' res:'. $res);
            $res = json_decode($res,true);
            if (isset($res['data']) && !empty($res['data'])) {
                if ($res['data']['status'] == 1) {
                    DB::table('finance_withdraw_order')
                        ->where(['order_sn' => $v->order_sn])
                        ->update(['status' => 1]);
                    Log::info($v->order_sn . '订单状态已更新,状态：' . 1);
                    $this->line($v->order_sn . '订单状态已更新');
                } else {
                    Log::info($v->order_sn . '订单状态：' . $res['data']['status']);
                    $this->line($v->order_sn . '订单状态：' . $res['data']['status']);
                }
            }
        }
        $this->line('更新完成');

    }

}